%%%%%%%%%%%%%%%%%
%%% Figure 8
%%%%%%%%%%%%%%%%%%%%


clear all; close all;
para; 
%Apply finer grid for greater numerical accuracy
qGrid=600;
iComp=20;
options = odeset('AbsTol',1e-11,'RelTol',1e-11);

deltaSpace=linspace(3,30,iComp);

%Comparative statics with respect to 1/\delta within deltaSpace
for comp=1:iComp

   delta=deltaSpace(comp);
   delta=1/delta;
   
   clear vF0 

   %Solve model, given 1/delta
   OptContract;
   SB;
   Solve;

   T=3;
   Time;

   betaEnd=vbetat(end);
    
    
    
    
   %Save Output in vector form
    
   
    
    vTau(comp)=tau0;
    vQ(comp)=q;
    vA0(comp)=a0;
    vABOut(comp)=aB;
    vBeta0(comp)=beta0;
    vBetaB(comp)=betaB;
    vTau0(comp)=tau0;
    vF0stat(comp)=F0-0.5*kappa*q^2;
    vFBstat(comp)=FB-0.5*kappa*q^2;
    vFracBeta(comp)=1-betaEnd/beta0;
    vDiffBeta(comp)=beta0-betaEnd;
    
   
    
    
end 

%Redo analysis for delta=0, i.e., 1/delta=infty
delta=0;
   
   
OptContract;
SB;
Solve;
Time;
betaT=vbetat(end);
fracBetaT=1-betaT/beta0;
F0stat=F0-0.5*kappa*q^2;
space=deltaSpace;


%Plot
close all;


fFigure=figure(1); hold on;
zeros=ones(length(space),1);

 subplot(2,3,4); hold on
 plot(space,smooth(vQ), 'k', 'Linewidth',3);
  plot([min(space) max(space)],[q q], 'r:', 'Linewidth',3);
 xlabel('$1/\delta$','Interpreter','latex')
 ylabel('$q^*$','Interpreter','latex')
 title('D: Screening','Interpreter', 'LaTex', 'FontSize',13)
 axis tight;
 
 

subplot(2,3,1); hold on
 plot(space,smooth(vA0), 'k', 'Linewidth',3);
   plot([min(space) max(space)],[a0 a0], 'r:', 'Linewidth',3);
 xlabel('$1/\delta$','Interpreter','latex')
 ylabel('$a_0$','Interpreter','latex')
title('A: Initial Monitoring','Interpreter', 'LaTex', 'FontSize',13)
axis tight;
 
 subplot(2,3,5); hold on
 plot(space,smooth(vTau0), 'k', 'Linewidth',3);
    plot([min(space) max(space)],[tau0 tau0], 'r:', 'Linewidth',3);
 xlabel('$1/\delta$','Interpreter','latex')
 ylabel('$\bar{\tau}$','Interpreter','latex')
  title('E: Exp. Time to Default','Interpreter', 'LaTex', 'FontSize',13)
 axis tight;
 
 subplot(2,3,2); hold on
 plot(space,smooth(vF0stat), 'k', 'Linewidth',3);
     plot([min(space) max(space)],[F0stat F0stat], 'r:', 'Linewidth',3);
 xlabel('$1/\delta$','Interpreter','latex')
 ylabel('$F_{0^-}$','Interpreter','latex')
 title('B: Total Surplus','Interpreter', 'LaTex', 'FontSize',13)
 axis tight;

 subplot(2,3,3); hold on;
 xlabel('$1/\delta$','Interpreter','latex')
  ylabel('$\beta_0$','Interpreter','latex')
  plot([min(space) max(space)],[beta0 beta0], 'r:', 'Linewidth',3);
 plot(space,smooth(vBeta0), 'k', 'Linewidth',3);
  title('C: Initial Retention','Interpreter', 'LaTex', 'FontSize',13)
 axis tight;

subplot(2,3,6); hold on
 plot(space,smooth(vFracBeta), 'k', 'Linewidth',3);
plot([min(space) max(space)],[fracBetaT fracBetaT], 'r:', 'Linewidth',3);
 xlabel('$1/\delta$','Interpreter','latex')
 ylabel('$1-\beta_{T}/\beta_0$','Interpreter','latex')
  title('F: Selloff Speed','Interpreter', 'LaTex', 'FontSize',13)
axis tight;

resize_plot(fFigure,12,5.25,13);


print('Maturity1','-depsc')
 
 